using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using UWBGL_XNA_Lib13;
using Microsoft.Xna.Framework;

namespace XNA_SceneNode
{
	public partial class SceneTreeControl : UserControl
	{
        private UWB_PrimitiveList mList = null;
  
		public SceneTreeControl()
		{
            InitializeComponent();
            BuildSceneTree(Program.getModel().GetRootNode());
            UWB_SceneNode n = Program.getModel().getActiveNode();
            InitializeXformAndPrim(n);
		}

        private void InitializeXformAndPrim(UWB_SceneNode n)
        {
            xFormInfoControl1.setSceneNode(n);
            UWB_Primitive p = n.getPrimitive();
            if (null == p)
            {
                primitiveControl1.Visible = false;
                PrimitiveSelectPanel.Visible = false;
            }
            else
            {
                primitiveControl1.Visible = true;
                mList = p as UWB_PrimitiveList;
                if (null != mList)
                {
                    PrimitiveSelectPanel.Visible = true;
                    EchoPrimitiveCount.Text = "0";
                    SliderPrimitiveSelect.SetRange(0, mList.count()-1);
                    SliderPrimitiveSelect.Value = 0;
                    SliderPrimitiveSelect.Update();
                    primitiveControl1.SetPrimitive(mList.primitiveAt(0));
                }
                else
                {
                    primitiveControl1.SetPrimitive(n.getPrimitive());
                    PrimitiveSelectPanel.Visible = false;
                }
            }
        }

        private void BuildSceneTree(UWB_SceneNode sceneNode) {
            TreeControl.BeginUpdate();
               TreeControl.Nodes.Add(sceneNode.getName());
               TreeNode n = TreeControl.Nodes[0];
               n.Tag = (object)sceneNode;
               sceneTreeHelper(sceneNode, ref n);
            TreeControl.EndUpdate();
        }

        private void sceneTreeHelper(UWB_SceneNode sceneNode, ref TreeNode parent)
        {            
            for (int i = 0; i < sceneNode.numChildren(); i++)
            {
                UWB_SceneNode n = sceneNode.getChildNode(i);
                parent.Nodes.Add(n.getName());
                TreeNode p = parent.Nodes[i];
                p.Tag = (object)n;
                sceneTreeHelper(n, ref p);
            }
        }

        private void TreeControl_AfterSelect(object sender, TreeViewEventArgs e)
        {
            UWB_SceneNode n = (UWB_SceneNode)e.Node.Tag;
            InitializeXformAndPrim(n);
        }

        private void SliderPrimitiveSelect_Scroll(object sender, EventArgs e)
        {
            int n = SliderPrimitiveSelect.Value;
            if (null != mList)
            {
                primitiveControl1.SetPrimitive(mList.primitiveAt(n));
                EchoPrimitiveCount.Text = n.ToString();
            }
        }

 
	}

}
